package com.heima.pojo.vo;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

import static cn.hutool.core.bean.BeanUtil.copyToList;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PageResponse<T> {

    /**
     * 页码
     */
    private String pageIndex;

    /**
     * 页大小
     */
    private String pageSize;

    /**
     * 总页数
     */
    private String totalPage;

    /**
     * 总数
     */
    private String totalCount;

    private List<T> currentPageRecords;

    public static <O, T> PageResponse<T> of(Page<O> page, Class<T> clazz) {
        PageResponse<T> pageResponse = new PageResponse<>();
        pageResponse.setPageIndex(String.valueOf(page.getCurrent()));
        pageResponse.setPageSize(String.valueOf(page.getSize()));
        pageResponse.setTotalPage(String.valueOf(page.getPages()));
        pageResponse.setTotalCount(String.valueOf(page.getTotal()));
        pageResponse.setCurrentPageRecords(copyToList(page.getRecords(), clazz));

        return pageResponse;
    }

    public static <T> PageResponse<T> of(Page<T> page) {
        PageResponse<T> pageResponse = new PageResponse<>();
        pageResponse.setPageIndex(String.valueOf(page.getCurrent()));
        pageResponse.setPageSize(String.valueOf(page.getSize()));
        pageResponse.setTotalPage(String.valueOf(page.getPages()));
        pageResponse.setTotalCount(String.valueOf(page.getTotal()));
        pageResponse.setCurrentPageRecords(page.getRecords());

        return pageResponse;
    }

}
